package com.zj.leetcode.easy.greed;

/**
 * @program: algorithm
 * @description: 买卖股票的最佳时机
 * 121 best-time-to-buy-and-sell-stock
 * @author: Zhang Bo
 * @create: 2022-01-14 17:05
 **/
public class BestTimeToBuyAndSellStock {
    public static void main(String[] args) {
        int[] prices = {1, 2};
        Solution solution = new BestTimeToBuyAndSellStock().new Solution();
        System.out.println(solution.maxProfit(prices));
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        /**
         * 最笨的方法吧
         *
         * @param prices
         * @return
         */
        public int maxProfit(int[] prices) {
            int length = prices.length;
            int max = 0;
            for (int i = 0; i < length; i++) {
                for (int j = i; j >= 0; j--) {
                    int sub = prices[i] - prices[j];
                    max = Math.max(sub, max);
                }
            }
            return max;
        }

        /**
         * [7,1,5,3,6,4]
         * @param prices
         * @return
         */
        public int maxProfit01(int[] prices) {
            int length = prices.length;
            int min = Integer.MAX_VALUE;
            int sub = 0;
            for (int i = 0; i < length; i++) {
                if (min > prices[i]) {
                    min = prices[i];
                } else {
                    sub = Math.max(prices[i] - min, sub);
                }
            }
            return sub;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
